<?php
require_once 'Zend/Controller/Plugin/Abstract.php';

class Custom_Controller_Plugin_PageUrl extends Zend_Controller_Plugin_Abstract {
	var $pageSize; // 每页多行
	var $columnCount; // 总行数
	var $totalPages; // 总页数
	var $startColumnCount; // 分页的启始行
	var $currentPage; // 获取当前页
	var $url;
	

	/**
	 * 初始化分页数据
	 * Enter description here ...
	 * @param unknown_type $totalRecord
	 * @param unknown_type $pageSize
	 * @param unknown_type $currentPage
	 */
	function __construct($totalRecord, $pageSize, $currentPage,$url) {
		$this->columnCount = $totalRecord;
		$this->pageSize = $pageSize;
		$this->currentPage = $currentPage;
		$this->url=$url;
		if (($this->columnCount % $this->pageSize) == 0) {
			$this->totalPages = $this->columnCount / $this->pageSize;
		} else {
			$this->totalPages = $this->columnCount / $this->pageSize + 1;
		}
		
		if (($this->currentPage) > $this->totalPages) {
			$this->startColumnCount = (($this->totalPages - 1) * $this->pageSize);
			$this->currentPage = $this->totalPages;
		} elseif ($this->currentPage > 1) {
			$this->startColumnCount = (($this->currentPage - 1) * $this->pageSize);
		} else {
			$this->startColumnCount = 0;
		}
	}
	
	function getPageMenu() {
		$menu = "";
		if ($this->currentPage>1) {
			$menu .= "<a href=\"";
			$menu .=$this->url;
			$menu .= $this->currentPage - 1;
			$menu .= "\" class=\"nextpostslink\">上一页</a>";
		}
		if ($this->currentPage < 6) {
			$j = 1;
			for($i = 1; $i <= 6 && $i < $this->totalPages; $i ++, $j ++) {
				if ($this->currentPage == $i) {
					$menu .= "<span class=\"current\">[";
					$menu .= $i;
					$menu .= "]</span>";
				} else {
					$menu .= "<a href=\"";
					$menu .=$this->url;
					$menu .= $i;
					$menu .= "\" class=\"page\">[";
					$menu .= $i;
					$menu .= "]</a>";
				}
			}
			if ($this->totalPages > $j) {
				$menu .= "...";
				$menu .= "<a href=\"";
				$menu .=$this->url;
				$menu .= $this -> totalPages;
				$menu .= "\" class=\"page\">[";
				$menu .= $this -> totalPages;
				$menu .= "]</a>";
			} elseif ($this->totalPages == $j) {
				$menu .= "<a href=\"";
				$menu .=$this ->  totalPages;
				$menu .=$this->url;
				$menu .= "\" class=\"page\">[";
				$menu .= $this ->  totalPages;
				$menu .= "]</a>";
			}
		} else {
			$menu .= "<a href=\"";
			$menu .=$this->url;
			$menu .= "1";
			$menu .= "\" class=\"page\">[";
			$menu .= "1";
			$menu .= "]</a>";
			$menu .= "...";
			$j = $this->currentPage - 4;
			for($i = $this->currentPage - 4; $i < $this->currentPage + 4 && $i <= $this->totalPages; $i ++, $j ++) {
				if ($this->currentPage == $i) {
					$menu .= "<span class=\"current\">[";
					$menu .= $this->currentPage;
					$menu .= "]</span>";
				} else {
					$menu .= "<a href=\"";
					$menu .=$this->url;
					$menu .= $i;
					$menu .= "\" class=\"page\">[";
					$menu .= $i;
					$menu .= "]</a>";
				}
			}
			if ($this->totalPages > $j) {
				$menu .= "...";
				$menu .= "<a href=\"";
				$menu .=$this->url;
				$menu .= $this->totalPages;
				$menu .= "\" class=\"page\">[";
				$menu .= $this-> totalPages;
				$menu .= "]</a>";
			} elseif ($this->totalPages == $j) {
				$menu .= "<a href=\"";
				$menu .=$this->url;
				$menu .= $this->totalPages;
				$menu .= "\" class=\"page\">[";
				$menu .= $this->totalPages;
				$menu .= "]</a>";
			}
		}
		
		if ($this->currentPage < $this->totalPages) {
			$menu .= "<a href=\"";
			$menu .=$this->url;
			$menu .= $this->currentPage + 1;
			$menu .= "\" class=\"nextpostslink\">下一页</a>";
		}
		$menu .= "<span class=\"pages\">";
		$menu .= "当前第";
		$menu .= $this->currentPage;
		$menu .= "页,共";
		$menu .= $this->totalPages;
		$menu .= "页";
		$menu .= "</span>";
		return $menu;
	
	}
}

